from django.http import JsonResponse
from django.shortcuts import render
from comment.errorcode import LogCodes

from utils.tokenutils import *

not_token_url = ["/user/login","/user/"]


def jwt_middleware(get_response):
    def middleware(request):
        token = request.GET.get("token")
        if  request.path not in not_token_url:
            if (token == None):
                return JsonResponse({"code": LogCodes.LOGIN_ERROR, "message": "params is not ful"})
            elif check_token(token) == False:
                return JsonResponse({"code": LogCodes.LOGIN_ERROR, "message": "token is timeout"})
        response = get_response(request)
        print(request.path)
        return response
    
    return middleware
